package com.jinsi.controller.tang;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.constant.TangConstant;
import com.jinsi.constant.YltConstant;
import com.jinsi.pojo.Commodity;
import com.jinsi.pojo.SyUser;
import com.jinsi.service.tang.CommodityReviewService;
import com.jinsi.service.tang.ExamineService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 审核商品列表
 * @author 唐文博
 * @Date 2022-08-15
 */
@RestController
@RequestMapping("/commodityReview")
@Slf4j
public class CommodityReviewController {
    @Autowired
    private CommodityReviewService commodityReviewService;

    @Autowired
    private ExamineService examineService;

    /**
     * 筛选搜索查询,根据商品名称、商品分类、上传人查询
     * @param page
     * @param limit
     * @param commodityName
     * @param classifyTitle
     * @param classifyId
     * @param userName
     * @return
     */
    @RequestMapping("/getCommodityReviewSee")
    public R getCommodityReviewSee(Integer page, Integer limit, String commodityName,
                                              String classifyTitle, Integer classifyId,
                                              String userName){
        PageHelper.startPage(page,limit);
        List<Commodity> list = commodityReviewService.getCommodityReviewSee(commodityName,classifyTitle,
                classifyId,userName);
        PageInfo<Commodity> pageInfo = new PageInfo<>(list);
        return R.custom(YltConstant.LAYUI_CODE,
                YltConstant.LAYUI_MSG,
                pageInfo.getList(),
                pageInfo.getTotal());
    }

    /**
     * 修改审核状态 1为已审核
     * @param commodityId
     * @param session
     * @return
     */
    @RequestMapping("/deleteCommodityReview")
    public R deleteCommodityReview(Integer commodityId, HttpSession session) {
        Date date = new Date();
        SyUser user = (SyUser) session.getAttribute("user");
        System.out.println(user.getUserName());
        try {
            log.info("修改审核状态码: [开始], 商品列表id:{}, 用户名称:{}",commodityId,session);
            commodityReviewService.deleteCommodityReview(commodityId);
            log.info("修改商品审核记录日志: [开始] 结果：{}","开始");
            examineService.addCkExamine(user.getUserName(),TangConstant.EXAMINE_EXAMINENORMAL,
                    TangConstant.EXAMINE_NORMAL,commodityId,date);
            log.info("修改商品审核记录日志: [结束] 结果：{}","成功");
            log.info("修改审核状态码: [结束] 结果：{}","成功");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改审核状态码: [失败]", e);
            return R.error();
        }
    }

    /**
     * 修改审核状态 驳回 为0 未通过 reject
     * @param commodityId
     * @param feedbackdetails
     * @param session
     * @return
     */
    @RequestMapping("/deleteReject")
    public R deleteReject(Integer commodityId,String feedbackdetails, HttpSession session) {

        Date date = new Date();
        SyUser user = (SyUser) session.getAttribute("user");
        System.out.println(user.getUserName());
        try {
            log.info("修改审核状态码: [开始], 商品列表id:{},名称：{}, 用户id:{}",commodityId,feedbackdetails,session);
            commodityReviewService.deleteReject(commodityId);
            log.info("修改商品审核记录日志: [开始] 结果：{}","开始");
            examineService.addCkExamine(user.getUserName(),TangConstant.EXAMINE_EXAMINEFAILED,
                   feedbackdetails,commodityId,date);
            log.info("修改商品审核记录日志: [结束] 结果：{}","成功");
            log.info("修改审核状态码: [结束] 结果：{}","成功");
            return R.ok("200");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改审核状态码: [失败]", e);
            return R.error();
        }
    }

    /**
     * 查看审核通过的商品  1
     * @param page
     * @param limit
     * @param commodityExamine
     * @return
     */
    @RequestMapping("/getCommodityReviewExamineNormal")
    public R getCommodityReviewExamineNormal(Integer page,Integer limit,Integer commodityExamine){
        PageHelper.startPage(page,limit);
        List<Commodity> list = commodityReviewService.getCommodityReviewExamineNormal(commodityExamine);
        PageInfo<Commodity> pageInfo = new PageInfo<>(list);
        return R.custom(YltConstant.LAYUI_CODE,
                YltConstant.LAYUI_MSG,
                pageInfo.getList(),
                pageInfo.getTotal());
    }
}
